<template>
  <a-layout style="min-height: 100vh">
    <a-layout-sider v-model:collapsed="collapsed" collapsible>
      <div class="logo" />
      <a-menu v-model:selectedKeys="selectedKeys" theme="dark" mode="inline">
        <a-menu-item key="admin-dashboard">
          <router-link to="/admin">
            <dashboard-outlined />
            <span>仪表盘</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="admin-users">
          <router-link to="/admin/users">
            <user-outlined />
            <span>会员管理</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="admin-plans">
          <router-link to="/admin/plans">
            <shopping-outlined />
            <span>套餐管理</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="admin-admins">
          <router-link to="/admin/admins">
            <team-outlined />
            <span>管理员</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="back-to-front">
          <router-link to="/">
            <rollback-outlined />
            <span>返回前台</span>
          </router-link>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <a-row>
          <a-col :span="18">
            <a-breadcrumb style="margin: 16px 24px">
              <a-breadcrumb-item>管理后台</a-breadcrumb-item>
              <a-breadcrumb-item>{{ currentPage }}</a-breadcrumb-item>
            </a-breadcrumb>
          </a-col>
          <a-col :span="6" style="text-align: right; padding-right: 24px">
            <a-dropdown>
              <a class="ant-dropdown-link" @click.prevent>
                管理员 <down-outlined />
              </a>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="0" @click="logout">
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </a-col>
        </a-row>
      </a-layout-header>
      <a-layout-content style="margin: 0 16px">
        <div :style="{ padding: '24px', background: '#fff', minHeight: '360px' }">
          <router-view />
        </div>
      </a-layout-content>
      <a-layout-footer style="text-align: center">
        智能提示词管理与分享平台 - 管理后台 ©2023
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
  DashboardOutlined,
  UserOutlined,
  ShoppingOutlined,
  TeamOutlined,
  RollbackOutlined,
  DownOutlined
} from '@ant-design/icons-vue'

const route = useRoute()
const router = useRouter()
const collapsed = ref(false)
const selectedKeys = ref([route.name])

// 监听路由变化，更新选中的菜单项
watch(
  () => route.name,
  (newVal) => {
    selectedKeys.value = [newVal]
  }
)

const currentPage = computed(() => {
  switch (route.name) {
    case 'admin-dashboard':
      return '仪表盘'
    case 'admin-users':
      return '会员管理'
    case 'admin-plans':
      return '套餐管理'
    case 'admin-admins':
      return '管理员'
    default:
      return ''
  }
})

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('isAdmin')
  router.push('/auth/login')
}
</script>

<style scoped>
.logo {
  height: 32px;
  margin: 16px;
  background: rgba(255, 255, 255, 0.3);
}
</style>